package com.wutianyi.images;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.microemu.app.capture.AnimatedGifEncoder;
import org.robotframework.abbot.editor.widgets.GifDecoder;

public class ImageUtils
{

    public static byte[] resizeGIF(byte[] src, int scaleX, int scaleY, int left, int top, int width, int height)
            throws FileNotFoundException
    {
        GifDecoder gifDecoder = new GifDecoder();
        if (GifDecoder.STATUS_FORMAT_ERROR == gifDecoder.read(new ByteArrayInputStream(src)))
        {
            System.out.println("Error");
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
        animatedGifEncoder.start(outputStream);
        animatedGifEncoder.setRepeat(gifDecoder.getLoopCount());
        animatedGifEncoder.setQuality(19);
        int frameCount = gifDecoder.getFrameCount();
        for (int i = 0; i < frameCount; i++)
        {
            BufferedImage sourceFrame = gifDecoder.getFrame(i);
            BufferedImage targetFrame = resize(sourceFrame, scaleX, scaleY, left, top, width, height);
            animatedGifEncoder.setDelay(gifDecoder.getDelay(i));
            animatedGifEncoder.addFrame(targetFrame);
        }
        animatedGifEncoder.finish();
        return outputStream.toByteArray();
    }

    public static byte[] resizeByType(byte[] src, int scaleX, int scaleY, int left, int top, int width, int height,
            String type) throws IOException
    {
        BufferedImage srcImage = ImageIO.read(new ByteArrayInputStream(src));
        BufferedImage target = resize(srcImage, scaleX, scaleY, left, top, width, height);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ImageIO.write(target, type, output);
        return output.toByteArray();
    }

    private static BufferedImage resize(BufferedImage source, int scaleX, int scaleY, int left, int top, int width,
            int height)
    {
        int type = source.getType();
        int targetW = scaleX;
        int targetH = scaleY;
        float sx = (float) scaleX / source.getWidth();
        float sy = (float) scaleY / source.getHeight();
        BufferedImage target = null;
        if (type == BufferedImage.TYPE_CUSTOM)
        {
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
        }
        else
        {
            target = new BufferedImage(targetW, targetH, type);
        }

        Graphics2D g = target.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));

        g.dispose();
        return target.getSubimage(left, top, width, height);
    }

    public static void main(String[] args) throws IOException
    {
        FileOutputStream output = new FileOutputStream("20090625020025712638164_new.bmp");
        byte[] bs = resizeByType(FileUtils.readFileToByteArray(new File("20090625020025712638164.bmp")), 106, 146, 0, 0, 106,
                146, "bmp");
        output.write(bs);
        output.flush();
        output.close();
        System.out.println("asfa.png".substring("asfa.png".lastIndexOf(".") + 1));
    }
}
